\documentclass[a4paper,12pt]{article}

\usepackage[margin=1.5cm]{geometry}
\usepackage{listings}
\usepackage{enumitem}

\title{Encode Video to PSP-compatible format}
\author{Chen Rushan\\chenrsster@gmail.com}
\date{2009.11.21 20:26}

\setlength{\parskip}{1.2ex}
\setlength{\parindent}{0pt}

\lstset{
    frame=single, framesep=0pt, framexleftmargin=-1pt, framextopmargin=3pt,
    framexbottommargin=3pt, aboveskip=0.5cm, belowskip=0.2cm, columns=flexible,
    basicstyle=\ttfamily
}

\begin{document}

\maketitle

\begin{lstlisting}
 Commands presented in this article are only tested on SONY PSP3000, so there's
 no guarantee that they will work on other kinds of PSP.
\end{lstlisting}

The most popular video format for PSP is MP4, and for PSP3000, two kinds of MP4
are supported, one is the standard MP4, the other is MP4-AVC. And note that not
all MP4 video files can play on PSP, they are subject to some constraints, here
I list some basic ones:

\begin{table}[h]
\centering
\begin{tabular}{|l|p{6cm}|p{6cm}|}
\hline
& \multicolumn{1}{c|}{standard MP4} & \multicolumn{1}{c|}{MP4-AVC} \\
\hline
resolution & no higher than the multiplication of 320x240, so 368:208 is ok, 
    since 76544 $<$ 76800 & width up to 480, height up to 272 \\
\hline
audio sample rate & lower than 24KHz & lower that 48KHz\\
\hline
\end{tabular}
\caption{MP4 constraints}
\label{tab:mp4cons}
\end{table}

From table \ref{tab:mp4cons}, we can easily find that the quality of MP4-AVC is
way better than standard MP4.

Video encoding generally involves three parts:

\begin{itemize}[topsep=0pt, itemsep=0pt]
    \item \textbf{audio encoding}

    \item \textbf{video encoding}

    \item \textbf{specifying the container format}
\end{itemize}

In this article I'll list some commands to convert video to be PSP-compatible,
and when it comes to MP4-AVC, I'll show two different tools, although one of
them will almost never be used in my future life.

\section{For standard MP4}

\label{sec:stanmp4}

To get a standard MP4 video, run: (numbers in all the following commands may
vary in different situations)

\begin{lstlisting}
 mencoder -af lavcresample=24000 -vf scale=368:208,harddup -oac lavc -ovc lavc \
        -lavcopts aglobal=1:vglobal=1:vcodec=mpeg4:vbitrate=900:acodec=libfaac \
        -of lavf -lavfopts format=psp -ofps 30000/1001 \
        -sub <subtitle-file> -o <output-video> <input-video>
\end{lstlisting}

(\textbf{Trem} field in the output means {\em time remaining})

This looks quite scary at first sight, but in fact, it's not very difficult to
understand, and you don't need to know all the detail information. Let's talk
about them one by one: (for more information, refer to manpage of mencoder or
google)

\begin{itemize}
    \item \textbf{-af lavcresample=24000}

        \textbf{-af} defines the audio filter, \textbf{lavcresample=24000} means
        set the audio sample rate to 24KHz.

    \item \textbf{-vf scale=368:208,harddup}

        \textbf{-vf} defines the video filter, \textbf{scale=368:208} means
        scale down the image to 368x208, \textbf{harddup} forces duplicate
        frames to be encoded in the output. This uses slightly more
        space, but is necessary for output to MPEG files. \textbf{harddup}
        should be placed at the end of the filter chain.

    \item \textbf{-oac lavc}

        \textbf{-oac} defines the audio codec. \textbf{lavc} means encoding with
        {\em libavcodec}. Note that {\em libavcodec} is a big collection
        of decoders and encoders for video and audio, it's not a specific codec,
        and you can choose among them using options \textbf{acodec} and
        \textbf{vcodec} of \textbf{-lavcopts}.

    \item \textbf{-ovc lavc}

        \textbf{-ovc} defines the video codec. \textbf{lavc} means encoding with
        {\em libavcodec}.

    \item \textbf{-lavcopts aglobal=1:vglobal=1:vcodec=mpeg4:vbitrate=900:acodec=libfaac}

        \textbf{-lavcopts} specifies options specific to {\em libavcodec}.
        \textbf{aglobal=1:vglobal=1} is a must for PSP MP4 video, without it,
        video will fail to play. \textbf{vcodec=mpeg4} specifies the specific
        video codec in {\em libavcodec}. \textbf{vbitrate=900} says that the
        video bitrate will be 900kbit/s.

    \item \textbf{-of lavf}

        \textbf{-of} specifies the container format, \textbf{lavf} means
        encoding with {\em libavformat}, which is a big collection of multimedia
        container formats(commonly-seen container formats include mp4, avi, rm,
        flv etc.), and you can use \textbf{format} option of \textbf{-lavfopts}
        to choose the one you need.

    \item \textbf{-lavfopts format=psp}

        This option chooses \textbf{psp} as the container format, it's actually
        the \textbf{mp4} container format with some extra information required
        by PSP.

    \item \textbf{-ofps 30000/1001}

        \textbf{-ofps} sets FPS(frames per second), common values are 24000/1001,
        30000/1001.

    \item \textbf{-sub $<$subtitle-file$>$}

        \textbf{-sub} specifies the subtitle file if you have one.

    \item \textbf{-o $<$output-video$>$}

        \textbf{-o} specifies the output video path.

\end{itemize}

Now let's make a option classification, as mentioned above, video encoding
generally involves there parts, so let's see which option belongs to which part:

\begin{itemize}
    \item \textbf{audio encoding}: -af, -oac, -lavcopts

    \item \textbf{video encoding}: -vf, -ovc, -lavcopts, -ofps

    \item \textbf{specifying the container format}: -of, -lavfopts
\end{itemize}

Now you might be very clear about the monster command.

\section{For MP4-AVC}

There are two tools you can use to convert video to MP4-AVC format:

\begin{lstlisting}
 HandBrakeCLI --verbose -i <input-video> --title 1 --chapters "1-1" \
        --maxWidth 480 --maxHeight 272 --encoder x264 --x264opts cabac=0 \
        --vb 768 --aencoder faac --ab 128 --arate 48 -o <output-video>
\end{lstlisting}

\begin{lstlisting}
 mencoder -af lavcresample=48000 -vf scale=480:272,harddup \
        -oac lavc -lavcopts abitrate=128:aglobal=1:acodec=libfaac \
        -ovc x264 -x264encopts bitrate=768:global_header \
        -of lavf -lavfopts format=psp -ofps 24000/1001 \
        -sub <subtitle-file> -o <output-video> <input-video>
\end{lstlisting}

One very big advantage \verb=mencoder= has over \verb=handbrake= is that burning
subtitle into video is always feasible using \verb=mencoder=, for
\verb=handbrake=, this is the case only when the source file is DVD. Besides
that, 480x272 is actually 480x(some\_number\_less\_than\_272) when using
\verb=handbrake=, so you'll see two tiny horizontal black bars when playing
video on PSP, and this is not the case for \verb=mencoder=.

I'm not going to delve into \verb=HandBrakeCLI= in this article, for more
information, just google.

The major difference between this \verb=mencoder= and the \verb=mencoder= in
section \ref{sec:stanmp4} is the video codec, here, we use the {\em
libx264} codec for video encoding, which is specified by \textbf{-ovc x264}, and
options for this codec is given after \textbf{-x264encopts}, among them,
\textbf{global\_header} is a must for PSP MP4 video.

Although I use relatively lower video bitrate and FPS compared to those used in
section \ref{sec:stanmp4}, the result video is still much better than standard
MP4.

\section{Background}

What is MP4?

An ISO/IEC standard 14496 developed by the Moving Picture Experts Group
(MPEG).MP4 is a new container format, a container format allows you to combine
different multimedia streams into one single file. MP4 is streamable and
supports all kinds of multimedia content.

\end{document}

